/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Facility;
import healthcare.coursework.model.Patient;
import healthcare.coursework.model.RequestStatus;
import healthcare.coursework.model.TestSamples;
import healthcare.coursework.service.AppointmentService;
import healthcare.coursework.service.FacilityService;
import healthcare.coursework.util.Utility;
import java.util.Date;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Timebox;
import org.zkoss.zul.Window;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Administrator
 */
public class EditSample extends GenericAutowireComposer {

    private AppointmentService appointmentService = util.Utility.getUtility().getAppointmentService();
    private FacilityService facilityService = util.Utility.getUtility().getFacilityService();
    private Textbox testName;
    private Combobox resourceName;
    private Datebox trackDate;
    private Label nameOfPatient;
    private Textbox result;
    private Window self;
    private Date previousTrackedTime;
    private TestSamples testSample;

    public void onLoadForm(Event e){
        setInfo();
    }

    private void setInfo() {
        assignInformation();
        setConstraint();
    }

    private void assignInformation() {
        testSample = (TestSamples) arg.get("TestSample");
        testName.setValue(testSample.getTestName());
        nameOfPatient.setValue(testSample.getPatient().getFullName());
        resourceName.setModel(new ListModelList(facilityService.listOfFacilities()));
        resourceName.setText(testSample.getFacility().getFacilityName());
        previousTrackedTime=new Date(testSample.getTrackedDate().getTime());
        trackDate.setValue(previousTrackedTime);
        result.setValue(testSample.getSampleResult());
    }

    private void setConstraint() {
        trackDate.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateAppointTime(cmpnt, o);
            }
        });
    }

    private void validateAppointTime(Component comp, Object value) throws WrongValueException {
        Date d = (Date) value;
        if (d.before(new Date()) || d.equals(new Date())) {
            throw new WrongValueException(comp, "The day of appointment must be greater than current date!!!");
        }
    }

    public void onCancel(Event e) {
        self.onClose();
        Executions.sendRedirect("TrackSamples.do");
    }

    public void onReset(Event e) {
        assignInformation();
    }

    public void onUpdate(Event e) {
        try {
            testSample.setSampleResult(result.getValue());
            testSample.setTestName(testName.getValue());
            testSample.setFacility((Facility) resourceName.getSelectedItem().getValue());
            Date testTime=trackDate.getValue();
            if(!testTime.equals(previousTrackedTime))
            {
                if(testTime.equals(previousTrackedTime))
                {
                    testSample.setTrackedDate(Utility.convertDateToTimestamp(trackDate.getValue()));
                }
                else
                testSample.setRequestStatus(RequestStatus.pending);
            }
            
            appointmentService.updateTestSample(testSample);
            Messagebox.show("Testsample of "+testSample.getPatient().getFullName()+" at time :"
                    + testSample.getTrackedDate().toString() + " has been modified!", "Information", Messagebox.OK, Messagebox.INFORMATION);
            self.onClose();
            Executions.sendRedirect("TrackSamples.do");
        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
